package com.ai.api.controller;

import com.ai.bo.AdminLoginBo;
import com.ai.grace.result.GraceJSONResult;
import com.ai.grace.result.ResponseStatusEnum;
import com.ai.service.AdminService;
import com.ai.utils.RedisOperator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wst.ai.pojo.admin.Admin;

@RestController
@RequestMapping("user")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisOperator redisOperator;
    @PostMapping("login")
    public GraceJSONResult login(@RequestBody AdminLoginBo adminLoginBo){
        Admin admin = adminService.login(adminLoginBo.getUserName(), adminLoginBo.getPassword());
        if (admin!= null){
            redisOperator.set("ADMIN_ID"+ ":",admin.getId());
            return GraceJSONResult.ok(admin);
        }else {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.ADMIN_LOGIN_ERROR);
        }
    }
    @PostMapping("getUserInfo")
    public GraceJSONResult getUserInfo(@RequestParam String userId){
        Admin admin = adminService.getUserInfo(userId);
        return GraceJSONResult.ok(admin);
    }

    @PostMapping("logout")
    public GraceJSONResult logout(){
        redisOperator.del("ADMIN_ID" + ":");
        return GraceJSONResult.ok();
    }
}
